<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>多个Promise对象</title>
</head>
<body>
  <script>
    /**
     * 1. Promise.all()
     * 2. Promise.race()
    */

    const pro1 = new Promise((resolve, reject) => {
      setTimeout(() => {
        resolve('2000ms');
      }, 2000);
    });

    const pro2 = new Promise((resolve, reject) => {
      setTimeout(() => {
        // resolve('500ms');
        reject('500ms');
      }, 500);
    });

    // 1. Promise.all()，所有的Promise实例都返回resolve时进入then，如果有一个返回reject则会进入catch
    Promise.all([pro1, pro2]).then((result) => {
      console.log('allResult', result);
    }).catch((err) => {
      console.log('allErr', err);
    });

    // Promise.race()，第一个返回的如果是resolve会进入then，第一个返回的是reject则进入catch
    Promise.race([pro1, pro2]).then((result) => {
      console.log('raceResult', result);
    }).catch((err) => {
      console.log('raceErr', err);
    });
  </script>
</body>
</html>